package com.partner.share.work.web.admin.util;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;

/**
 * @author pengyangyan
 * @version 1.0.0
 * @date 2019年10月08日 10:07:00
 */
public class ExcelStyleUtil implements IExcelExportStyler {


    private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
    private static final short FONT_SIZE_TEN = 10;
    private static final short FONT_SIZE_ELEVEN = 11;
    private static final short FONT_SIZE_TWELVE = 12;

    /**
     * 大标题样式
     */
    private CellStyle headStyle;

    /**
     * 每行标题样式
     */
    private CellStyle titleStyle;

    /**
     * 数据行样式
     */
    private CellStyle styles;

    public ExcelStyleUtil(Workbook workbook){
        this.init(workbook);
    }

    private void init(Workbook workbook){
        this.headStyle = this.initHeaderStyle(workbook);
        this.titleStyle = this.initTitleStyle(workbook);
        this.styles = this.initStyles(workbook);
    }

    private CellStyle initHeaderStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
        return style;
    }

    private CellStyle initTitleStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
        //背景色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return style;
    }

     private CellStyle initStyles(Workbook workbook) {
         CellStyle style = getBaseCellStyle(workbook);
         style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
         style.setDataFormat(STRING_FORMAT);
         return style;
    }


     /**
      * 基础样式
      *
      * @return
      */
     private CellStyle getBaseCellStyle(Workbook workbook) {
         CellStyle style = workbook.createCellStyle();
         //下边框
         style.setBorderBottom(BorderStyle.THIN);
         //左边框
         style.setBorderLeft(BorderStyle.THIN);
         //上边框
         style.setBorderTop(BorderStyle.THIN);
        //右边框
         style.setBorderRight(BorderStyle.THIN);
         //水平居中
         style.setAlignment(HorizontalAlignment.CENTER);
         //上下居中
         style.setVerticalAlignment(VerticalAlignment.CENTER);
         //设置自动换行
        style.setWrapText(true);
        return style;
     }

      /**
       * 字体样式
       *
       * @param size   字体大小
       * @param isBold 是否加粗
       * @return
       */
     private Font getFont(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
     }


    @Override
    public CellStyle getHeaderStyle(short i) {
        return this.headStyle;
    }

    @Override
    public CellStyle getTitleStyle(short i) {
        return this.titleStyle;
    }

    @Override
    public CellStyle getStyles(boolean b, ExcelExportEntity excelExportEntity) {
        return getStyles(true,excelExportEntity);
    }

    @Override
    public CellStyle getStyles(Cell cell, int i, ExcelExportEntity excelExportEntity, Object o, Object o1) {
        return this.styles;
    }

    @Override
    public CellStyle getTemplateStyles(boolean b, ExcelForEachParams excelForEachParams) {
        return null;
    }
}
